/**
 * Created by Administrator on 2017/5/17 0017.
 */
$(function () {

    /* */
    $(document).on('mouseenter', '.element', function() {
        $(this).popover('show');
    });
    $(document).on('mouseleave', '.element', function() {
        $(this).popover('hide');
    });

    var _form = $("#form-send");

    // 表单ajax提交
    _form.submit(function () {
        $("#loading").show();
        var _this = $(this);
        $.ajax({
            type : 'POST',
            url : _this.attr('action'),
            data : _this.serialize(),
            dataType : 'json',
            success : function (msg) {
                layer.msg(msg.msg, {icon: 6,time:1000});
                setTimeout(function () {
                    if (msg.code == 1) {
                        if (msg.url) { // 有链接就跳连接
                            window.location.href = msg.url;
                        }
                        if (msg.data == 'reload') { // data 的值等于 reload  刷新当前页面
                            window.location.reload();
                        }
                        parent.location.reload();  // 刷新主页面
                    }
                    $("#loading").hide();
                }, 500);
            }
        });
        return false;
    });


    /*删除数据*/
    $(document).on("click", ".delete", function () {
        var _this = $(this);
        layer.confirm('确定要删除吗？',function(){
            window.location.href = _this.data('href');
        });
    });
    /*审核通过*/
    $(document).on("click", ".is_ok", function () {
        var _this = $(this);
        layer.confirm('确定允许通过吗？',function(){
            window.location.href = _this.data('href');
        });
    });
    /*审核失败*/
    $(document).on("click", ".is_no", function () {
        var _this = $(this);
        layer.confirm('确定不允通过吗？',function(){
            window.location.href = _this.data('href');
        });
    });



    /*修改状态*/
    $(document).on("click", ".td-status a", function () {
        var _this = $(this);
        if (!_this.data('title')) {
            return false;
        }
        layer.confirm(_this.data('title'),function(){
            window.location.href = _this.data('href');
        });
    });

    /*单张图片上传*/
    var $image = $(".image-crop > img");
    $($image).cropper({
        aspectRatio: SCOPE.cropper_aspect_ratio,
        preview: ".img-preview",
        done: function(data) {
            // Output the result data for cropping image.
        }
    });

    var $inputImage = $("#inputImage");
    if (window.FileReader) {
        $inputImage.change(function() {
            var fileReader = new FileReader(), files = this.files, file;
            if (!files.length) {
                return;
            }
            file = files[0];

            if (/^image\/\w+$/.test(file.type)) {
                console.log(file);
                fileReader.readAsDataURL(file);
                fileReader.onload = function () {
                    $inputImage.val("");
                    $image.cropper("reset", true).cropper("replace", this.result);
                };
            } else {
                showMessage("Please choose an image file.");
            }
        });
    } else {
        $inputImage.addClass("hide");
    }

    var $inputFile = $("#inputFile");
    if (window.FileReader) {
        $inputFile.change(function() {
            var fileReader = new FileReader(), files = this.files, file;
            if (!files.length) {
                return;
            }
            file = files[0];

                console.log(file);
        });
    } else {
        $inputFile.addClass("hide");
    }

    $("#image_close").click(function () {
        window.location.reload();
    });
    /*单张图片上传 -- end*/


    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });

    /*分类树配置*/
    SCOPE.category_ztree_setting = {
        view: {
            dblClickExpand: false,
            showLine: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable:true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            beforeClick: function(treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                if (treeNode.isParent) {
                    zTree.expandNode(treeNode);
                    return false;
                } else {
                    zTree.selectNode(treeNode);
                    $("input[name='category_id']").val(treeNode.id);
                    return true;
                }
            }
        }
    };
    /*分类树配置 -- end*/

    /*输入框失去焦点之后修改字段的值*/
    $(".field").blur(function () {
        $("#loading").show();
        var _this = $(this);
        if (_this.val() == _this.data('default-val')) {
            $("#loading").hide();
            return false;
        }
        var data = {
            id : _this.parents('tr').data('id'),
            model : _this.parents('tr').data('model'),
            field : _this.attr('name'),
            value : _this.val()
        };
        console.log(data);
        $.ajax({
            type : 'POST',
            url : SCOPE.save_model_field_url,
            data : data,
            dataType : 'json',
            success : function (msg) {
                console.log(msg);
                $("#loading").hide();
            }
        });
    });
    /*输入框失去焦点之后修改字段的值  --  end  */
});